//
//  ChangePasswordViewController.m
//  阿帕
//
//  Created 阿帕 on 2019/12/24.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "ChangePasswordViewController.h"

@interface ChangePasswordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *oldTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *xinTextField;
@property (weak, nonatomic) IBOutlet UITextField *againTextField;
@property (weak, nonatomic) IBOutlet UIButton *chageButton;

@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self baseLayout];
}

- (void)baseLayout {
 
    self.navigationItem.title = @"修改密码";
    
    self.view.backgroundColor = BackgroundColor;
    
//    _oldTextFiled.textFieldType = CJTextFieldStringTypePassword;
    _oldTextFiled.maxLength = 18;
    
//    _xinTextField.textFieldType = CJTextFieldStringTypePassword;
    _xinTextField.maxLength = 18;
    
//    _againTextField.textFieldType = CJTextFieldStringTypePassword;
    _againTextField.maxLength = 18;
    
    cut(_chageButton, BTN_BORDER_RADIUS);
    [_chageButton setBackgroundColor:Theme_ColorValue];
}

- (IBAction)confirmBtnClick:(UIButton *)sender {
    WEAKSELF
    
    if (_oldTextFiled.text.length > 20 || _xinTextField.text.length > 20 || _againTextField.text.length > 20 ) {
        [XSInfoView showInfo:@"密码最长为20位" onView:self.view];
        return;
    }
    if (![_xinTextField.text isEqual:_againTextField.text]) {
        [XSInfoView showInfo:@"输入的两次新密码不一致" onView:self.view];
        return;
    }
    if ([DCSpeedy isBlankString:_oldTextFiled.text] == YES || [DCSpeedy isBlankString:_xinTextField.text] == YES || [DCSpeedy isBlankString:_againTextField.text] == YES) {
        [XSInfoView showInfo:@"输入的信息不完整" onView:self.view];
        return;
    }
    
    NSDictionary *params = @{
//        @"oldPwd": _oldTextFiled.text,
//        @"newPwd": _xinTextField.text,
        @"oldPwd": [DCSpeedy base64:_oldTextFiled.text],
        @"newPwd": [DCSpeedy base64:_xinTextField.text],
        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
    };
    
    [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], PARTY_UPDATEPASS) withParameter:params withVC:self netName:@"修改密码" success:^(id response, NSString *msg) {
        
        [XSInfoView showInfo:@"修改密码成功,需重新登录" onView:self.view];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
            
            [UserDefaultUtil saveValue:@"" forKey:USER_TOKEN];
            
            [KNotificationCenter postNotificationName:LOGIN_LOGOUT object:@{}];
            
            [KNotificationCenter postNotificationName:@"AuthOverdue" object:nil];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            });
        });
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

- (IBAction)showOrHiddenOldPsdEvent:(UIButton *)sender {
    if (sender.selected) {
        [sender setImage:image(@"gcy_icon_close_eye") forState:UIControlStateNormal];
        _oldTextFiled.secureTextEntry = YES;
        
    } else {
        [sender setImage:image(@"gcy_icon_open_eye") forState:UIControlStateNormal];
        _oldTextFiled.secureTextEntry = NO;
    }
    sender.selected = !sender.selected;
}

- (IBAction)showOrHiddenNewPsdEvent:(UIButton *)sender {
    if (sender.selected) {
        [sender setImage:image(@"gcy_icon_close_eye") forState:UIControlStateNormal];
        _xinTextField.secureTextEntry = YES;
        _againTextField.secureTextEntry = YES;
    } else {
        [sender setImage:image(@"gcy_icon_open_eye") forState:UIControlStateNormal];
        _xinTextField.secureTextEntry = NO;
        _againTextField.secureTextEntry = NO;
    }
    sender.selected = !sender.selected;
}

@end
